#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
typedef struct _Student{
        int sid;
        char name[20];
}Student;
int main(int argc, char const *argv[])
{
        if(argc != 2){
                printf("./a.out filename\n");
                return -1;
        }
        //open
        int fd = open(argv[1],O_RDWR|O_CREAT|O_TRUNC,0666);
        int length = sizeof(Student);
        //truncate file size
        ftruncate(fd, length);
        //mmap
        Student* stu = mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
        if(stu == MAP_FAILED){
                perror("mmap err");
                return -1;
        }
        int num = 1;
        //修改内存数据
        while (1){
                stu->sid=num;
                sprintf(stu->name,"小明-%03d",num++);
                sleep(1);//每隔1s修改一次映射区的内容
        }
        //释放映射区和关闭文件描述符
        munmap(stu, length);
        close(fd);
        return 0;
}
